﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCResources.General;
using SCResources.BusinissLayer;
using Serie_Counter.Forms;


namespace Serie_Counter.Presentatie
{   
    /// <summary>
    /// user control serieCounter
    /// </summary>
    public partial class serieUserControl : UserControl
    {
        private Serie serie;
        private ScResources manager;
        private reDrawScreen reDrawFormScreen;
        private SelectedCollection selectedCollection;
        private updateForm updateForm;

        /// <summary>
        /// usercontrol constructor
        /// </summary>
        /// <param name="Titel">serie title</param>
        /// <param name="Season">season</param>
        /// <param name="Episode">episode</param>
        /// <param name="reDrawFormScreen">redraw delegate</param>
        /// <param name="removeSerie">remove delegate</param>
        /// <param name="USerie">update delegate</param>
        /// <param name="selectedCollection">true = active, false = archived</param>
        public serieUserControl(Serie serie, ScResources manager, reDrawScreen rDC, updateForm updateForm, SelectedCollection selectedCollection = SelectedCollection.Active)
        {
            InitializeComponent();

            labelSerieTitel.Text = serie.Titel;
            labelSeasonNr.Text = serie.Season.ToString();
            labelEpisodeNr.Text = serie.Episode.ToString();
            labelAirdayDisplay.Text = serie.NextAirDate.ToShortDateString();
            labelFrequencyNr.Text = serie.Frequency.ToString();

            if(serie.CurSeason != 0 && serie.CurEpisode != 0)
            {
                labelCurAired.Text = string.Format("(S: {0} E: {1})", serie.CurSeason.ToString(),
                                                   serie.CurEpisode.ToString());
            }
            else
            {
                labelCurAired.Visible = false;
            }

            this.serie = serie;
            this.updateForm = updateForm;

            if(selectedCollection.Equals(SelectedCollection.Active))
            {
                buttonArchOrAct.Text = "Archive";
                buttonArchOrAct.Click += new EventHandler(buttonArchive_Click);
            }
            else
            {
                buttonArchOrAct.Text = "Activate";
                buttonArchOrAct.Click += new EventHandler(buttonActivate_Click);
            }

            this.reDrawFormScreen = rDC;
            this.manager = manager;
            this.selectedCollection = selectedCollection;

            //get cover
            setCover();

        }

        private void setCover()
        {
            pictureBoxCover.SizeMode = PictureBoxSizeMode.Zoom;
            try
            {
                pictureBoxCover.Image = Image.FromFile(serie.CoverPath);
            }
            catch (Exception)
            {

                try
                {
                    pictureBoxCover.Image = Image.FromFile(GeneralVars.DEFAULT_COVER_PATH);
                }
                catch (FileNotFoundException e)
                {
                    MessageBox.Show("Default cover pic not found!" + e.Message, "Cover error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            }
        }

        private void buttonEpisodeUp_Click(object sender, EventArgs e)
        {
            serie.Episode++;
            labelEpisodeNr.Text = serie.Episode.ToString();
            
            manager.updateSerie(serie,selectedCollection);
            
            
        }

        private void buttonSeasonUp_Click(object sender, EventArgs e)
        {
            serie.Season++;
            serie.Episode = 1;
            labelEpisodeNr.Text = serie.Episode.ToString();
            labelSeasonNr.Text = serie.Season.ToString();

            //todo: think what to do with the remember function if series is over
            //current serie options:
            serie.CurEpisode = 1;
            serie.CurSeason++;
            labelCurAired.Text = string.Format("(S: {0} E: {1})", serie.CurSeason.ToString(),
                                                   serie.CurEpisode.ToString());
            //save changes
            manager.updateSerie(serie, selectedCollection);
            
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            
            manager.removeSerie(serie.Titel, selectedCollection);
            
            reDrawFormScreen();
        }

        private void buttonArchive_Click(object sender, EventArgs e)
        {
            manager.moveSerieActiveToArchived(serie);
            reDrawFormScreen();
            updateForm(selectedCollection);
        }

        private void buttonActivate_Click(object sender, EventArgs e)
        {
            manager.moveSerieArchivedToActive(serie);
            reDrawFormScreen();
            updateForm(selectedCollection);
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
           // SerieForm updateSerie = new SerieForm(manager,serie,reDrawFormScreen,selectedCollection);
            //updateSerie.Show();
        }
        
    }
}
